package com.jrx.anytxn.param;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;

/**
 * @program: anytxn-cf-transaction
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-03-27 14:12
 **/
public class CreateBatchXml {

    public static void main(String args[]) throws Exception {

        transfer("com.jrx.anytxn.param.entity.BtLimitAdjustInfo");

    }

    public static void transfer(String className) throws ClassNotFoundException {
        Class a = Class.forName(className);
        StringBuilder sb = new StringBuilder();
        sb.append("<update id=\"batchUpdateForBatch\" parameterType=\"ArrayList\">\n");
        sb.append("update ").append(ModelToTable(a.getSimpleName())).append("\n");
        sb.append("<trim prefix=\"set\" suffixOverrides=\",\">\n");
        Field[] fields = a.getDeclaredFields();
        for(Field field : fields){
            if("id".equals(propertyToField(field.getName()))){
                continue;
            }
            if("create_time".equals(propertyToField(field.getName()))){
                continue;
            }
            if("update_time".equals(propertyToField(field.getName()))){
                continue;
            }
            sb.append("<trim prefix=\"").append(propertyToField(field.getName())).append("=case\" suffix=\"else ").append(propertyToField(field.getName())).append(" end,\">\n");
            sb.append("<foreach collection=\"list\" item=\"i\" index=\"index\">\n");
            sb.append("<if test=\"i.").append(field.getName()).append("!=null\">\n");
            sb.append("when id=#{i.id} then #{i.").append(getFiledName(field.getName())).append("}\n");
            sb.append("</if>\n");
            sb.append("</foreach>\n");
            sb.append("</trim>\n");
        }
        sb.append("</trim>\n");
        sb.append("where id in\n");
        sb.append("<foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n");
        sb.append("#{item.id,jdbcType=BIGINT}\n");
        sb.append("</foreach>\n");
        sb.append("</update>\n");

        System.out.println(sb);
    }

    public static String ModelToTable(String property) {
        if (null == property) {
            return "";
        }
        char[] chars = property.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : chars) {
            if (CharUtils.isAsciiAlphaUpper(c)) {
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString().substring(1,sb.length());
    }

    public static String propertyToField(String propertys) {
        String[] propertyArray = propertys.split(".");
        String property = propertys;
        if(propertyArray.length>0){
            property = propertyArray[propertyArray.length-1];
        }

        if (null == property) {
            return "";
        }
        char[] chars = property.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : chars) {
            if (CharUtils.isAsciiAlphaUpper(c)) {
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String getFiledName(String propertys) {
        String[] propertyArray = propertys.split(".");
        String property = propertys;
        if(propertyArray.length>0){
            property = propertyArray[propertyArray.length-1];
        }
        return property;
    }
}
